package com.osdp.simple.routes;

//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.gateway.route.RouteLocator;
//import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.MediaType;
//import org.springframework.web.reactive.function.BodyInserters;
//import org.springframework.web.reactive.function.server.RequestPredicates;
//import org.springframework.web.reactive.function.server.RouterFunction;
//import org.springframework.web.reactive.function.server.RouterFunctions;
//import org.springframework.web.reactive.function.server.ServerResponse;
//import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.concurrent.TimeUnit;

//@Configuration
public class RoutesHandler {
    /*public static final String HELLO_FROM_FAKE_ACTUATOR_METRICS_GATEWAY_REQUESTS = "hello from fake /actuator/metrics/gateway.requests";

    @Value("${test.uri:http://httpbin.org:80}")
    String uri;

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        //@formatter:off
        // String uri = "http://httpbin.org:80";
        // String uri = "http://localhost:9080";
        System.out.println("----uri: "+ uri);
        return builder.routes()
                .route("path_route", r -> r.path("/get")
                        .uri("http://httpbin.org"))
                .route(r -> r.host("**.abc.org").and().path("/anything/png")
                        .filters(f ->
                                f//.prefixPath("/httpbin") //prefixPath一个过滤器，可用于在网关路由请求之前为请求的路径添加前缀。
                                        .addResponseHeader("X-TestHeader", "foobar"))
                        //.uri(uri)
                        .uri("http://httpbin.org")
                )
                .route("read_body_pred", r -> r.host("*.readbody.org")
                                .and().readBody(String.class,
                                s -> s.trim().equalsIgnoreCase("hi"))
                                .filters(f -> f.prefixPath("/httpbin")
                                        .addResponseHeader("X-TestHeader", "read_body_pred"))
                                .uri(uri)
//					.uri("http://httpbin.org")
                )
                .route("rewrite_request_obj", r -> r.host("*.rewriterequestobj.org")
                                .filters(f -> f//.prefixPath("/httpbin")
                                        .addResponseHeader("X-TestHeader", "rewrite_request")
                                        .modifyRequestBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE,
                                                (exchange, s) -> {
                                                    return Mono.just(new Hello(s.toUpperCase()));
                                                })
                                )
//					.uri(uri)
                                .uri("http://httpbin.org")
                )
                .route("rewrite_request_upper", r -> r.host("*.rewriterequestupper.org")
                                .filters(f -> f.prefixPath("/httpbin")
                                        .addResponseHeader("X-TestHeader", "rewrite_request_upper")
                                        .modifyRequestBody(String.class, String.class,
                                                (exchange, s) -> {
                                                    return Mono.just(s.toUpperCase() + s.toUpperCase());
                                                })
                                )
                                .uri(uri)
//					.uri("http://httpbin.org")
                )
                .route("rewrite_response_upper", r -> r.host("*.rewriteresponseupper.org")
                                .filters(f -> f.prefixPath("/httpbin")
                                        .addResponseHeader("X-TestHeader", "rewrite_response_upper")
                                        .modifyResponseBody(String.class, String.class,
                                                (exchange, s) -> {
                                                    System.out.println("--------s: "+ s);
                                                    return  Mono.just(s.toUpperCase());
                                                })
                                ).uri(uri)
//					.uri("http://httpbin.org")
                )
                .route("rewrite_response_obj", r -> r.host("*.rewriteresponseobj.org")
                        .filters(f -> f//.prefixPath("/httpbin")
                                .addResponseHeader("X-TestHeader", "rewrite_response_obj")
                                .modifyResponseBody(Map.class, String.class, MediaType.TEXT_PLAIN_VALUE,
                                        (exchange, map) -> {
                                            Object data = map.get("data");
                                            return Mono.just(data.toString());
                                        })
                                .setResponseHeader("Content-Type", MediaType.TEXT_PLAIN_VALUE)
                        )//.uri(uri)
                        .uri("http://httpbin.org")
                )
                .route(r -> r.path("/image/webp")
                        .filters(f ->
                                f//.prefixPath("/httpbin")
                                        .addResponseHeader("X-AnotherHeader", "baz"))
                        //.uri(uri)
                        .uri("http://httpbin.org")
                )
                .route(r -> r.order(-1)
                        .host("**.throttle.org").and().path("/get")
                        .filters(f -> f//.prefixPath("/httpbin")
                                .filter(new ThrottleGatewayFilter()
                                        .setCapacity(1)
                                        .setRefillTokens(1)
                                        .setRefillPeriod(10)
                                        .setRefillUnit(TimeUnit.SECONDS)))
                        //.uri(uri)
                        .uri("http://httpbin.org")
                )
                .build();
        //@formatter:on
    }

    @Bean
    public RouterFunction<ServerResponse> testFunRouterFunction() {
        RouterFunction<ServerResponse> route = RouterFunctions.route(
                RequestPredicates.path("/testfun"),//针对给定的路径模式，返回测试请求路径的RequestPredicate
                request -> ServerResponse.ok().body(BodyInserters.fromValue("hello")));
        return route;
    }

    @Bean
    public RouterFunction<ServerResponse> testWhenMetricPathIsNotMeet() {
        RouterFunction<ServerResponse> route = RouterFunctions.route(
                RequestPredicates.path("/actuator/metrics/gateway.requests"),
                request -> ServerResponse.ok().body(BodyInserters
                        .fromValue(HELLO_FROM_FAKE_ACTUATOR_METRICS_GATEWAY_REQUESTS)));
        return route;
    }

    static class Hello {

        String message;

        Hello() {
        }

        Hello(String message) {
            this.message = message;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

    }*/
}
